package 图;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/6/13 15:01
 */
public class lc797所有可能的路径 {



    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        List<List<Integer>> ans = new ArrayList<>();
        Deque<Integer> path = new ArrayDeque<>();
        path.addLast(0);
        dfs(graph,ans,path,0);
        return ans;
    }
    // used[] 都用不到, 1. 无环, 2. 有向
    private void dfs(int[][] graph, List<List<Integer>> ans, Deque<Integer> path, int st) {
        // basecase  no need
        if(st == graph.length - 1){
           ans.add(new ArrayList<>(path));
            return ;
        }
        for(int next : graph[st]){
            path.addLast(next);
            dfs(graph,ans,path,next);
            path.removeLast();
        }
    }


    /*
     下面这个 dfs 的方式也能过!
     */

    public List<List<Integer>> allPathsSourceTargetOne(int[][] graph) {
        List<List<Integer>> ans = new ArrayList<>();
        Deque<Integer> path = new ArrayDeque<>();
//        path.addLast(0);
        dfsOne(graph,ans,path,0);
        return ans;
    }
    // used[] 都用不到, 1. 无环, 2. 有向
    private void dfsOne(int[][] graph, List<List<Integer>> ans, Deque<Integer> path, int st) {
        path.addLast(st);
        if(st == graph.length - 1){
            ans.add(new ArrayList<>(path));
            return ;
        }
        for(int next : graph[st]){
            dfsOne(graph,ans,path,next);
            path.removeLast();
        }
    }



}
